package com.hogan.basiclibrary.base.baseApi.api;

import com.hogan.basiclibrary.base.baseApi.interceptor.ParamsInterceptor;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import static com.hogan.basiclibrary.base.baseApi.api.BaseHttpApiMethods.DEFAULT_TIME;
import static com.hogan.basiclibrary.base.baseApi.api.BaseHttpApiMethods.getCache;
import static com.hogan.basiclibrary.base.baseApi.api.BaseHttpApiMethods.getCacheInterceptor;
import static com.hogan.basiclibrary.base.baseApi.api.BaseHttpApiMethods.getHttpLoggingInterceptor;

/**
 * Created by zhy on 15/8/17.
 */
public class OkHttpUtils {

    private static OkHttpUtils mInstance;
    private OkHttpClient.Builder mOkHttpClient;

    public OkHttpUtils() {

        if (null == mOkHttpClient) {
            mOkHttpClient = new OkHttpClient.Builder();
            mOkHttpClient.connectTimeout(DEFAULT_TIME, TimeUnit.SECONDS);
            mOkHttpClient.readTimeout(DEFAULT_TIME, TimeUnit.SECONDS);
            mOkHttpClient.writeTimeout(DEFAULT_TIME, TimeUnit.SECONDS);
            mOkHttpClient.cache(getCache())
                    //    .addInterceptor(new HeadInterceptor())//根据需要设置，别乱设置
                    .addInterceptor(new ParamsInterceptor())
                    .addInterceptor(getHttpLoggingInterceptor())
                    .addInterceptor(getCacheInterceptor())
                    .addNetworkInterceptor(getCacheInterceptor())
                    .build();
        }
    }

    public static OkHttpUtils getInstance() {
        if (mInstance == null) {
            synchronized (OkHttpUtils.class) {
                if (mInstance == null) {
                    mInstance = new OkHttpUtils();
                }
            }
        }
        return mInstance;
    }


    public void cancelTag(Object tag) {
        for (Call call : mOkHttpClient.build().dispatcher().queuedCalls()) {
            if (tag.equals(call.request().tag())) {
                call.cancel();
            }
        }
        for (Call call : mOkHttpClient.build().dispatcher().runningCalls()) {
            if (tag.equals(call.request().tag())) {
                call.cancel();
            }
        }
    }


    public void get(String url,String tag, final HoganCall hoganCall) {
        final Request request = new Request.Builder()
                .url(url)
                .tag(tag)
                .build();
        Call call = mOkHttpClient.build().newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                if (null != hoganCall) {
                    hoganCall.onFailure(e.toString());
                }
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (null != hoganCall) {
                    String body = response.body().string();
                    hoganCall.onSuccess(body);
                }
            }
        });

    }

    public interface HoganCall {

        void onFailure(String error);

        void onSuccess(String body);
    }
}

